<?php
//------------------------
// 前台用户地址控制器
//-------------------------

namespace app\home\controller;

use app\common\controller\HomeBase;
use app\common\model\UserAddress as UserAddressModel;
use think\Session;




class UserAddress extends HomeBase
{
    protected $UserAddressModel;



	protected function _initialize()
    {
        parent::_initialize();
        $this->UserAddressModel              = new UserAddressModel();

    }



    // 添加新地址
    public function add(){
        $user_id = Session::get('user_id');
        if (!$user_id) {
            $this->error('非法操作');
        }
        return $this->fetch('add');
    }

    // 添加新地址操作
    public function addProc(){
        $user_id = Session::get('user_id');
        if (!$user_id) {
            $this->error('非法操作');
        }

        $data = $this->request->param();

         $validate_result = $this->validate($data, 'UserAddress');

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        $data['user_id'] = $user_id;

        $res = $this->UserAddressModel->save($data);

        if ($res === false) {
            $this->error($this->UserAddressModel->getError());
        } else {
            $this->success('添加成功');
        }
    }

    // 编辑地址
    public function edit(){
        $user_id = Session::get('user_id');
        if (!$user_id) {
            $this->error('非法操作');
        }
        $id = $this->request->param('id',0,'intval');

        $data = $this->UserAddressModel->get($id);

        $this->assign('data',$data);

        return $this->fetch('edit');
    }

    // 编辑操作
    function editProc(){
        $data  = $this->request->param('');

        $validate_result = $this->validate($data, 'UserAddress');

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        $res = $this->UserAddressModel->allowField(true)->save($data,$data['id']);

        if ($res === false) {
            $this->error($this->UserAddressModel->getError());
        } else {
            $this->success('保存成功');
        }
    }


    // 删除操作
    public function delete(){
        $user_id = Session::get('user_id');
        if (!$user_id) {
            $this->error('非法操作');
        }

        $id = $this->request->param('id',0,'intval');

        $where['id'] = array('eq',$id);

        $res = $this->UserAddressModel->where($where)->delete();

        if ($res === false) {
            $this->error($this->UserAddressModel->getError());
        } else{
            $this->success('删除成功');
        }
    }

    // 设置默认操作
    public function setDefalut(){
        $user_id = Session::get('user_id');
        if (!$user_id) {
            $this->error('非法操作');
        }

        $id = $this->request->param('id',0,'intval');

        // 先将其他当前用户的地址设置默认值为0
        $this->UserAddressModel->where('user_id',$user_id)->update(['is_default' => 0]);

        $res = $this->UserAddressModel->where('id',$id)->update(['is_default' => 1]);

        if ($res === false) {
            $this->error($this->UserAddressModel->getError());
        } else{
            $this->success('设置成功');
        }
    }





}
